﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ExplorerTreeView.ViewModel;
using System.IO;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;

namespace CSharp.Libs.WPF.UserControls
{
    #region event handlers
    public delegate void ExplorerTreeViewSelectionChangedEventHandler(object sender, ExplorerTreeViewSelectionChangedEventArgs e);
    public class ExplorerTreeViewSelectionChangedEventArgs : EventArgs
    {
        public IList SelectedItems { get; set; }
    }
    #endregion

    
    
    /// <summary>
    /// Interaktionslogik für ExplorerTreeView.xaml
    /// </summary>
    public partial class ExplorerTreeView : UserControl
    {
        #region declarations
        #region event handling
        public event ExplorerTreeViewSelectionChangedEventHandler ExplorerTreeViewSelectionChanged;
        protected void OnExplorerTreeViewSelectionChanged(
            ExplorerTreeViewSelectionChangedEventArgs ea)
        {
            if (ExplorerTreeViewSelectionChanged != null)
            {
                ExplorerTreeViewSelectionChanged(this, ea);
            }
        }
        #endregion
        //public event EventHandler SelectionChanged;

        TreeItemViewModel _rootNode = new TreeItemViewModel(null, false) { DisplayName = "Computer" };
        #endregion



        #region constructor
        public ExplorerTreeView()
        {
            InitializeComponent();

            _getDriveNodes();

            DataContext = _rootNode;

        }
        #endregion



        #region private functions
        private void _getDriveNodes()
        {
            DriveInfo[] diDrives = DriveInfo.GetDrives();

            foreach (DriveInfo diDrive in diDrives)
            {
                TreeItemViewModel driveNode = new TreeItemViewModel(_rootNode, true) ;
                driveNode.DisplayName = diDrive.Name;
                driveNode.PathFullName = diDrive.Name;
                _rootNode.Children.Add(driveNode);
            }
        }
        #endregion



        #region eventhandling
        private void TheTreeView_SourceUpdated(object sender, DataTransferEventArgs e)
        {
            //Sorting here?
            //TheTreeView.Items.SortDescriptions.Clear();
            //TheTreeView.Items.SortDescriptions.Add(new SortDescription("DisplayName", ListSortDirection.Ascending));
        }

        //private void ExplorerTreeView_PreviewSelectionChanged(object sender, ExplorerTreeViewSelectionChangedEventArgs e)
        //{
        //    OnSelectionChanged();
        //}
        //private void ExplorerTreeView_SelectionChanged(object sender, ExplorerTreeViewSelectionChangedEventArgs e)
        //{
        //    OnSelectionChanged();
        //}

        //protected void OnSelectionChanged()
        //{
        //    var handler = SelectionChanged;
        //    if (handler != null)
        //    {
        //        ExplorerTreeViewSelectionChangedEventArgs e = new ExplorerTreeViewSelectionChangedEventArgs();
        //        e.SelectedItems = this.SelectedItems;
        //        handler(this, e);
        //    }
        //}

        private void TheTreeView_SelectionChanged(object sender, EventArgs e)
        {
            OnExplorerTreeViewSelectionChanged(new ExplorerTreeViewSelectionChangedEventArgs() { SelectedItems = this.SelectedItems });
        }
        #endregion



        #region properties
        public IList SelectedItems
        {
            get
            {
                return TheTreeView.SelectedItems;
            }
            set
            {
                TheTreeView.SelectedItems = value;
            }
        }
        #endregion
    }
}
